var express = require('express');
var router = express.Router();
let db = require("../db/db");
  
/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});
// 注册接口
router.get('/register', function(req, res, next) {
  let usermsg = req.query;
  if(usermsg.name ==""||usermsg.name==undefined || usermsg.password==""||usermsg.password==undefined){
      var data = {
        code:0,
        msg:"注册失败,用户名或密码不能为空",
      }
      res.json(data);
  }else{
    let sql = `insert into user values (null,"${usermsg.name}","${usermsg.password}")`;
    db(sql,(err,result)=>{
      let data = {
        code:1,
        msg:"注册成功",
        data:result
      }
      res.send(data);
    })
  }
});
//登录
router.get('/login', function(req, res, next) {
  let usermsg = req.query;
  let name = usermsg.name;
  let password = usermsg.password;
  let sql = `select * from user where name="${name}" and password = "${password}"`;
  db(sql,(err,result)=>{
      if(err){
        let data={
          code:0,
          msg:"登录失败"
        }
        res.json(data)
      }else{
        let data = {
          code:1,
          msg:"登录成功",
          id:result[0].id
        }
        res.json(data)
      }

  })
  
});


module.exports = router;
